const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
const { createProxyMiddleware } = require('http-proxy-middleware');  

const app = express();
const port = 3100;
const api_port = 3110; //后端接口端口

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const storage = multer.memoryStorage();
const upload = multer({ storage: storage });

// API endpoint for /committed/togo
/* app.post('/committed/togo', upload.single('targetFiles'), (req, res) => {
    try {
        // Process the uploaded file (req.file)
        // You can access the file using req.file.buffer

        // Example: Log the content of the uploaded file
        console.log(req.file.buffer.toString());

        // Send a success response
        res.status(200).json({
            code: 200,
            msg: 'Request successful',
            data: { message: 'File processed successfully' }
        });
    } catch (error) {
        // Handle errors
        console.error(error);

        // Send an error response
        res.status(400).json({
            code: 400,
            msg: 'Request error',
            data: { message: 'Error processing the file' }
        });
    }
}); */

// Serve the HTML/css/js file
app.use('/public', express.static('public'));
app.use('/css', express.static('css'));
app.use('/js', express.static('js'));
app.use('/assets', express.static('assets'));

// 创建代理中间件
const proxy = createProxyMiddleware({
    target: `http://localhost:${api_port}`,
    changeOrigin: true,
    pathRewrite: {
        '^/to/api': '/committed/togo',
    }
});

app.use('/to/*', proxy);

app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});
